package com.kuang.config;

import com.kuang.srpingcloud.api.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class DeptConsumerCcontroller {
    //消费者不应该有service层
    //RestTemplate提供多种便捷远程访问的方法,查看RestTemplate源码，发现没注册到spring
    //String url, Class<T> responseType, Map<String,
    @Autowired
    private RestTemplate restTemplate;

    private static final String REST_URL_PREFIX="http://localhost:8001/dept";

    @RequestMapping("consumer/dept/queryByID/{id}")
    public Dept getById(@PathVariable("id") Long id){
        return restTemplate.getForObject(REST_URL_PREFIX+"/queryByID/"+id,Dept.class);
    }
    @RequestMapping("consumer/dept/add")
    public boolean add(Dept dept){
        return restTemplate.postForObject(REST_URL_PREFIX+"/add",dept,Boolean.class);
    }
    @RequestMapping("consumer/dept/queryAll")
    public List<Dept> queryAll(){
        return restTemplate.getForObject(REST_URL_PREFIX+"/queryAll",List.class);
    }
}
